<?php

namespace app\index\controller;

use app\admin\model\SiteConfig;
use app\admin\model\Sort;
use app\common\model\Ad;
use app\index\model\Question;
use app\common\model\User;
use think\Controller;
use think\facade\Session;
use think\Request;

class Index extends Base
{
    //首页
    public function index()
    {
        $sort = new Sort();
        //分类数据
        $sort_list = $sort->getsortlist();
        //seo数据
        $site_info = seo();
        //首页试题 - 只获取十条
        $question_list = Question::limit(10)->order('Id desc')->with('profile')->select();
        //dump($question_list);
        $question_num = Question::count();
        $this->assign([
            'site_seo' => $site_info,
            'sort_list' => $sort_list,
            'question_list' => $question_list,
            'question_num' => $question_num
        ]);
        return $this->fetch('index/index');
    }
    //前台 - 分类聚合
    public function classify()
    {
        $sort = new Sort();
        //分类数据
        $sort_list = $sort->getsortlist();
        //seo数据
        $site_info = seo();
        $this->assign([
            'site_seo' => $site_info,
            'sort_list' => $sort_list
        ]);

        return $this->fetch();
    }
    //前台 - 分类页
    public function cate()
    {
        $sorts = new Sort;
        $alias = $this->request->param('type');
        //判断是否是二级分类
        $is_c = $sorts->is_sort_c($alias);
        if ($is_c != false) {
            //二级 跳转到二级地址
            return $this->redirect('index/index/cate', ['type' => $is_c, 'c' => $alias]);
        }
        // echo request()->param('c');
        $alias_c = request()->param('c');
        if (isset($alias_c)) {
            $this->assign('sort_c', '');
            //二级目录
            $sort_info = $sorts->gets($alias_c);
        } else {
            //分类seo渲染
            $sort_info = $sorts->gets($alias);
            $this->assign('sort_c', $sorts->gets_c($alias));
        }
        if ($sort_info == false) {
            return $this->redirect('index/index/i_404');
        }
        //dump($sort_info);
        $que = new Question;
        //试题数据
        $list = $que->get_list($sort_info['Id'], 15);
        //最热推荐
        $hot_list = $que->get_hotList($sort_info['Id']);
        //更新seo
        $site_info  = seo($sort_info['name'], $sort_info['key'], $sort_info['desc'], 1);
        //dump($list);
        $page = $list->render();
        if (count($list) < 1) {
            $this->assign([
                'no_data' => '1'
            ]);
        } else {
            $this->assign([
                'no_data' => '0'
            ]);
        }
        $this->assign([
            'site_seo' => $site_info,
            'sort_info' => $sort_info,
            'list' => $list,
            'hot_list' => $hot_list,
            'page' => $page
        ]);

        return $this->fetch();
    }
    //前台 - 试题页面
    public function ask()
    {
        $question = new Question();
        $id = $this->request->param('id/d');
        //查询指定试题信息
        $info = $question->getone($id);

        if ($info == false) {
            return $this->redirect('index/index/i_404');
        }
        $this->assign('soft_a', '');
        $sorts = new Sort;
        if ($info['profile']['fid'] != 0) {
            $sort_a = $sorts->gets_id($info['profile']['fid']);
            $this->assign('sort_a', $sort_a);
        }
        //增加浏览量
        $question->look($id);
        //试题数据
        $list = $question->get_list($id, 15);
        //最热推荐
        $hot_list = $question->get_hotList($info['type']);

        $site_info = seo($info['title'], '', $info['option'], 1, 1);
        $this->assign([
            'site_seo' => $site_info,
            'question_info' => $info,
            'hot_list' => $hot_list
        ]);
        return $this->fetch();
    }
    //前台 - 登陆页
    public function login()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            //验证码
            if (!captcha_check($data['verify'])) {
                return json_x(-1, '验证码输入错误！');
            }
            // 验证数据
            $rule = [
                'username|用户名' => 'require|alphaDash|length:3,20',
                'password|密码' => 'require|length:3,20',
            ];
            $result = $this->validate($data, $rule);
            if (true !== $result) {
                return json_x(-1, $result);
            }

            $u = new User();
            if ($u->login($data['username'], $data['password'])) {
                return json_x(0, '登陆成功');
            } else {
                return json_x(-1, '登陆失败(请检查账号密码是否正确)');
            }
        } else {
            $site_info = seo('登陆账号', '', '', 1);
            $this->assign([
                'site_seo' => $site_info
            ]);
            return $this->fetch();
        }
    }
    //前台 - 注册页
    public function reg()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            //验证码
            if (!captcha_check($data['verify'])) {
                return json_x(-1, '验证码输入错误！');
            }
            // 验证数据
            $rule = [
                'username|用户名' => 'require|alphaDash|length:3,20',
                'password|密码' => 'require|length:3,20',
            ];
            $result = $this->validate($data, $rule);
            if (true !== $result) {
                return json_x(-1, $result);
            }

            $u = new User();
            if ($u->reg($data)) {
                return json_x(0, '注册成功');
            } else {
                return json_x(-1, '注册失败');
            }
        } else {
            $site_info = seo('注册账号', '', '', 1);
            $this->assign([
                'site_seo' => $site_info
            ]);
            return $this->fetch();
        }
    }
    //前台 退出登陆
    public function out()
    {
        Session::delete('main_user');
        return $this->redirect('index/index/index');
    }
    //404
    public function i_404()
    {
        return $this->fetch();
    }
}
